package br.com.naybee.client.views.add.configuracao;

import br.com.naybee.client.NaybeeService;
import br.com.naybee.client.views.add.menu.configuracao.MenuConfiguracaoView;
import br.com.naybee.client.views.base.Principal;
import br.com.naybee.framework.client.componente.gwt.event.CommEvent;
import br.com.naybee.framework.client.componente.gwt.event.CommHandler;
import br.com.naybee.framework.client.componente.gwt.web.InputText;
import br.com.naybee.framework.client.models.Model;
import br.com.naybee.framework.client.models.Models;
import br.com.naybee.framework.client.utils.NAYException;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class CadastrarTipoAutomacaoView extends Composite {

	private static CadastrarGrupoRepartimentoViewUiBinder uiBinder = GWT.create(CadastrarGrupoRepartimentoViewUiBinder.class);

	interface CadastrarGrupoRepartimentoViewUiBinder extends UiBinder<Widget, CadastrarTipoAutomacaoView> {
	}

	@UiField
	protected InputText descricao;

	public CadastrarTipoAutomacaoView() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@UiHandler("btnSalvar")
	public void onClickBtnSalvar(ClickEvent event) {
		String descricaoTipoAutomacao = descricao.getValue().toUpperCase();
		if (!descricaoTipoAutomacao.equalsIgnoreCase("")) {
			Model model = Models.newModel().add("descricaoAutomacao", descricaoTipoAutomacao);
			salvar(model);
		}
	}

	private void salvar(Model model) {
		NaybeeService naybeeService = new NaybeeService("tipoAutomacaoFs", "registrarTipoDeAutomacao");
		naybeeService.post(model, new CommHandler() {
			public void onSuccess(CommEvent event) {
				Principal.get().add(new MenuConfiguracaoView());
			}

			public void onException(NAYException error) {
			}
		});
	}

}
